<template>
    <div class="common-layout">
      <el-container>
        <el-header class="header-wrapped">
          <div class="header-content">
            <h3>妙咖</h3>
            <span class="welcome">欢迎您的登录</span>
          </div>
        </el-header>
        <el-main>
          <div class="login-wrapped">
            <el-card class="box-card">
              <el-tabs :stretch="true" v-model="activeName" class="demo-tabs" @tab-click="handleClick">
                <el-tab-pane label="登录" name="first">
                  <el-form class="login-form" :model="loginData" label-width="auto" style="max-width: 600px">
                    <el-form-item label="账号">
                      <el-input v-model="loginData.account" placeholder="请输入账号"/>
                    </el-form-item>
                    <el-form-item label="密码">
                      <el-input v-model="loginData.password" placeholder="请输入密码"/>
                    </el-form-item>
                    <div class="footer-wrapped">
                      <div class="forget-password">
                        <span class="forget-password-button" @click="openForgetPassword">忘记密码</span>
                      </div>
                      <div class="footer-button">
                        <el-button type="primary" @click="Login">登录</el-button>
                      </div>
                      <div class="footer-go-register">
                        还没有账号?<span class="go-register">立即注册</span>
                      </div>
                    </div>
                  </el-form>
                </el-tab-pane>
                <el-tab-pane label="注册" name="second">
                  <el-form class="login-form" :model="registerData" label-width="auto" style="max-width: 600px">
                    <el-form-item label="账号">
                      <el-input v-model="registerData.account" placeholder="请输入6-12位账号"/>
                    </el-form-item>
                    <el-form-item label="密码">
                      <el-input v-model="registerData.password" placeholder="密码需要长度请输入6-12位字母数字"/>
                    </el-form-item>
                    <el-form-item label="确认密码">
                      <el-input v-model="registerData.repassword" placeholder="请再次输入" />
                    </el-form-item>
                    <div class="footer-wrapped">
                      <div class="footer-button">
                        <el-button type="primary" @click="Register">注册</el-button>
                      </div>
                    </div>
                  </el-form>
                </el-tab-pane>
              </el-tabs>
            </el-card>
          </div>
        </el-main>
        <el-footer class="footer-wrapped">
          <div class="footer-content">
            <div class="title">
              <span>阿里云博客专家</span> | 
              <span>阿里云博客专家</span> | 
              <span>阿里云博客专家</span>
            </div>
          </div>
        </el-footer>
      </el-container>
    </div>
    <forget ref="forgetP"/>
</template>
<script lang="ts" setup>
import {reactive, ref} from "vue";
import forget from "./components/forget_password.vue"
const activeName = ref("first");
import { login, register } from "@/api/login";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
import {useLocalStorageSet} from "@/methods/crypto"
const router = useRouter();
import { useUserInfo } from "@/store/userinfo"
const store = useUserInfo()
interface formData {
  account: string;
  password: string;
  repassword?: string;
}

const loginData: formData = reactive({
  account: "",
  password: "",
})

const registerData: formData = reactive({
  account: "",
  password: "",
  repassword: "",
})
const handleClick = () => {

}

const forgetP = ref()
const openForgetPassword = () => {
  forgetP.value.open()
}

const Login = async() => {
  const res = await login(loginData);
  if (res.data.token) {
    ElMessage({
        message: res.data.msg,
        type: "success"
      })
      console.log(res.data.data.id)
      // useLocalStorageSet("token", res.data.token);
      store.updateUserInfo(res.data.data);
      router.push("/home");
  } else {
    ElMessage({
      message: res.data.msg,
      type: "warning"
    })
  }
}

const Register = async() => {
  if (registerData.password === registerData.repassword) {
    const res = await register(registerData)
    console.log(res,'res')
    if (res.data.code === 0) {
      ElMessage({
        message: res.data.msg,
        type: "success"
      })
      activeName.value = "first"
    } else {
      ElMessage({
        message: res.data.msg,
        type: "warning"
      })
    }
  } else {
    ElMessage({
        message: "请确认两次密码是否输入一致",
        type: "warning"
    })
  }
}
</script>
<style lang="scss" scoped>
// 头部外壳
.header-wrapped {
  // 头部内容
  .header-content {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    height: 100%;
    align-items: center;
    h3 {
      margin-right: auto;
    }
    .welcome {
      display: block;
      font-size: 13px;
    }
  }
}
.el-main {
  background-image: url("https://tse4-mm.cn.bing.net/th/id/OIP-C.m3d1BhAODiyA-FTRjD5MZgHaCs?rs=1&pid=ImgDetMain");
  background-size: 100%;
  height: 600px;
  --el-main-padding: 0;
  .login-wrapped {
    width: 1200px;
    height: 600px;
    margin: 0 auto;
    .box-card {
      width: 350px;
      height: 375px;
      float: right;
      position: relative;
      top: 14%;
      :deep(.el-tabs__item) {
        color: #333;
        font-size: 18px;
      }
      :deep(.el-input__inner) {
        height: 40px;
      }
      :deep(.el-form-item__label) {
        height: 40px;
        line-height: 40px;
      }
      .login-form {
        .footer-wrapped {
          display: flex;
          flex-direction: column;
          .forget-password {
            display: flex;
            justify-content: flex-end;
            margin: 10px 0;
            .forget-password-button {
              font-size: 12px;
              color: #409eff;
              cursor: pointer;
            }
          }
          .footer-button {
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            :deep(.el-button) {
              width: 300px;
              height: 45px;
              font-size: 16px;
            }
          }
          .footer-go-register {
            font-size: 12px;
            margin: 12px 0;
            display: flex;
            justify-content: center;
            .go-register {
              font-size: 12px;
              color: #409eff;
              cursor: pointer;
            }
          }
        }
      }
      .el-form {
        margin-top: 20px;
      }
    } 
  }
}

.footer-wrapped {
  margin-top: 10px;
  .footer-content {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    justify-content: center;
    align-items: center;
    .title {

    }
    span {
      color: #666;
      font-size: 12px;
    }
  }
}
</style>